package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.UserKit;
import com.sinbader.xiaole.common.entity.Supplier;
import com.sinbader.xiaole.common.enums.NumberUtilEnum;
import com.sinbader.xiaole.common.mapper.SupplierMapper;
import com.sinbader.xiaole.common.model.params.SupplierParam;
import com.sinbader.xiaole.common.model.result.SupplierResult;
import  com.sinbader.xiaole.common.service.SupplierService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinbader.xiaole.common.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 供应商 服务实现类
 * </p>
 *
 * @author LuoYong
 * @since 2020-04-16
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Override
    public void add(SupplierParam param){
        Supplier entity = getEntity(param);
        entity.setSupplierNum(StringUtil.getIDNumber(NumberUtilEnum.厂家.getName()));
        entity.setCreateTime(new Date());
        entity.setCreateUser(UserKit.getUserId());
        this.save(entity);
    }

    @Override
    public void delete(SupplierParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(SupplierParam param){
        Supplier oldEntity = getOldEntity(param);
        Supplier newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateTime(new Date());
        newEntity.setUpdateUser(UserKit.getUserId());
        this.updateById(newEntity);
    }

    @Override
    public SupplierResult findBySpec(SupplierParam param){
        return null;
    }

    @Override
    public List<SupplierResult> findListBySpec(SupplierParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SupplierParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(SupplierParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Supplier getOldEntity(SupplierParam param) {
        return this.getById(getKey(param));
    }

    private Supplier getEntity(SupplierParam param) {
        Supplier entity = new Supplier();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
